<!--<template>-->
<!--  <HModal title="取值" :visible="visible" @ok="handleOK" @cancel="close">-->
<!--    <FieldDelegate />-->
<!--  </HModal>-->
<!--</template>-->
<script>
import FieldDelegate from '@comp/hhd/design/FormEngine/FieldDelegate.vue';
import HModal from '@comp/hhd/HModal/HModal.vue';
import FieldRegistry from '@comp/hhd/design/FormField/FieldRegistry';
import FieldComponentFormulaTypes
  , {
  getFieldComponentFormula
} from '@comp/hhd/design/FormDesigner/field/comps/form/FieldValueFormulaGroups/configs/FieldComponentFormulaTypes';
import {stop} from '@/utils/FormDesignUtils';
import {AuthType} from '@comp/hhd/design/Constants';

export default {
  name: 'FieldValueGetter',
  components: {FieldDelegate},
  render(h) {
    return h(HModal, {
      props: {
        width: 800,
        height: 600,
        title: '取值',
        visible: this.visible,
      },
      attrs: {
        'data-role': 'ignore-select-field',
      },
      on: {
        ok: () => this.handleOk(),
        cancel: () => this.close(),
      }
    }, [this.renderField(h)]);
  },
  data() {
    return {
      visible: false,
      fieldConfig: undefined,
      value: undefined,
      title: undefined,
    };
  },
  methods: {
    handleOk() {
      if (!this.value) {
        this.$message.warn('请输入数值');
        return;
      }
      let constant = {
        variableName: this.value,
        variableType: getFieldComponentFormula(this.fieldConfig),
        variableTitle: this.title
      }
      this.$emit('addConst', constant)
      this.close();
    },
    renderField(h) {
      if (!this.fieldConfig) return undefined;
      let comp = FieldRegistry[this.fieldConfig.component];
      if (!comp) return undefined;
      return h(comp, {
        props: {
          auth: AuthType.edit,
          fieldConfig: this.fieldConfig,
          value: this.value,
          title: this.title,
        },
        on: {
          'update:value': v => this.value = v,
          'update:title': t => this.title = t
        }
      });
    },
    setData({fieldConfig}) {
      this.fieldConfig = fieldConfig;
      this.$nextTick(() => {
        this.open();
      });
    },
    open() {
      this.visible = true;
      this.value = undefined;
      this.title = undefined;
    },
    close() {
      this.visible = false;
    },
  }
};
</script>